package xyz.wadewhy.basicstest.juc;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static java.lang.Thread.sleep;

/**
 * @ClassName ThreadDemoV4
 * @Description 业务用途
 * @Author zhongzihao
 * @Date 2022/1/5 11:59 上午
 * @Version 1.0
 **/
@Slf4j(topic = "c.Test")
public class ThreadDemoV4 {
    @SneakyThrows
    public static void main(String[] args) {
//        packDemo();
        packDemo2();
    }

    /**
     * 13:12:54.198 [线程1] INFO c.Test - 线程1 begin
     * 13:12:55.210 [线程1] INFO c.Test - 线程1 park
     * 13:12:56.198 [main] DEBUG c.Test - unpark...
     * 13:12:56.198 [线程1] INFO c.Test - 线程1 resume
     * @throws InterruptedException
     */
    public static void packDemo() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.info("线程1 begin");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("线程1 park");
            LockSupport.park();
            log.info("线程1 resume");
        }, "线程1");
        t1.start();
        sleep(2000);
        log.debug("unpark...");
        LockSupport.unpark(t1);
    }

    public static void packDemo2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.info("线程1 begin");
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("线程1 park");
            LockSupport.park();
            log.info("线程1 resume");
        }, "线程1");
        t1.start();
        sleep(1000);
        log.debug("unpark...");
        LockSupport.unpark(t1);
    }
}
